{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "空"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[30, 4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "97"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "F"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "F"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "F"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入字符串134，225\n",
      "Counter({'2': 2, '1': 1, '3': 1, '4': 1, '，': 1, '5': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "h = input(\"请输入字符串\")\n",
    "frequencies = Counter(h)\n",
    "print(frequencies)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串:112345\n",
      "[('2', 2), ('3', 3), ('4', 4), ('5', 5)]\n"
     ]
    }
   ],
   "source": [
    "x=input('请输入一个字符串:')\n",
    "positions=[(ch,index) for index,ch in enumerate(x) if x.index(ch)==x.rindex(ch)]\n",
    "print(positions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dacasd\n",
      "char:a,last site:4\n",
      "char:c,last site:3\n",
      "char:s,last site:5\n"
     ]
    }
   ],
   "source": [
    "dict1={'a':0,'b':0,'c':0,'f':0,'h':0,'s':0}\n",
    "num=0\n",
    "def function(a,dict1,num):\n",
    "    for i in a:\n",
    "        num+=1\n",
    "        if i in dict1:\n",
    "            dict1[i]=num\n",
    "    return dict1\n",
    "a=str(input())\n",
    "function(a,dict1,num)\n",
    "for x in dict1:\n",
    "    if dict1[x]!=0:\n",
    "        print(\"char:{},last site:{}\".format(x,dict1[x]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表:{1,3},{3,5},{5,8}\n",
      "{1, 3, 5, 8}\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "last=eval(input('请输入一个列表:'))\n",
    "print(reduce(lambda x,y:x|y,last))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串：1234321\n",
      "yes\n"
     ]
    }
   ],
   "source": [
    "str1=input('请输入一个字符串：')\n",
    "str2=str1[::-1]\n",
    "if str1==str2:\n",
    "    print('yes')\n",
    "else:\n",
    "    print('no')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.Python3.x的range()函数返回一个___可迭代对象____\n",
    "\n",
    "2.表达式“[3]in[1,2,3,4]”的值为__False____\n",
    "\n",
    "3.列表对象的sort()方\n",
    "\n",
    "法用来对列表元素进行原地排序，该函数返回值为___None_____\n",
    "\n",
    "4.列表对象的__remove()__方法删除首次出现的指定元素，如果列表中不存在要删除的元素，则抛出异常。\n",
    "\n",
    "5.假设列表对象aList的值为[3,4,5,6,7,9,11,13,15,17]，那么切片aList[3:7]得到的值是___[6,7,9,11]______\n",
    "\n",
    "6.在Python中，字典和集合都是用一对___大括号___作为界定符，字典的每个元素由两部分组成，即__“键”和“值”,其中“键”__不允许重复。\n",
    "\n",
    "7.使用字典对象的__items()__方法可以返回字典的“键：值”对，使用字典对象的__keys()__方法可以返回字典的“键”，使用字典对象的___values()___方法可以返回字典的“值”。\n",
    "\n",
    "8.假设有列表a=['name','age','sex']和b=['Dong',38,'Male'],使用一条语句将这两个列表的内容转换为字典，并且以列表a中的元素为“键”，以列表b中的元素为“值”，这个语句可以写为__c=dict(zip(a,b))_________\n",
    "\n",
    "9.假设有一个列表a，现要求从列表a中每三个元素取一个，并且将取到的元素组成新的列表b，可以使用语句___b=a[::3]_______\n",
    "\n",
    "10.使用列表推导式生成包含10个数字5的列表，语句可以写为_[5 for i in range(10)]________。\n",
    "\n",
    "11.不可以 使用del命令来删除元组中的部分元素。\n",
    "\n",
    "12.已知列表data中有若干字符串，要求编写程序，对data中的字符串进行过滤，只输出重复字符不超过一半的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
